package com.bangbang.shiro;


import com.bangbang.exception.LoginException;
import com.bangbang.mapper.TbAdminMapper;
import com.bangbang.mapper.TbStudentUserMapper;
import com.bangbang.mapper.TbTeacherMapper;
import com.bangbang.pojo.TbAdmin;
import com.bangbang.pojo.TbStudentUser;
import com.bangbang.pojo.TbTeacher;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;



public class LoginRealm extends AuthorizingRealm {

    @Autowired
    private TbStudentUserMapper tbStudentUserMapper;

    @Autowired
    private TbTeacherMapper tbTeacherMapper;

    @Autowired
    private TbAdminMapper tbAdminMapper;

//    我们编写授权逻辑的方法
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        return null;
    }
//   我们编写认证逻辑的方法
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
//        多态
        UsernamePasswordToken token = (UsernamePasswordToken) authenticationToken;
//        获取用户名,并根据用户名查询用户信息
        if (token.getHost() == "1"){
         QueryWrapper<TbStudentUser> tbStudentUserMapperQueryWrapper = new QueryWrapper<>();
         tbStudentUserMapperQueryWrapper.eq("user_account", token.getUsername());
         TbStudentUser tbStudentUser = tbStudentUserMapper.selectOne(tbStudentUserMapperQueryWrapper);
         if (tbStudentUser == null){

            return null;
         }
        //1.登录成功后的user对象 2.数据库的密码 3.登录成功后的username
        return new SimpleAuthenticationInfo(tbStudentUser,tbStudentUser.getUserPassword(), ByteSource.Util.bytes(tbStudentUser.getUserSalt()),tbStudentUser.getUserAccount());
      }else if (token.getHost() == "2"){
            QueryWrapper<TbTeacher> teacherQueryWrapper = new QueryWrapper<>();
            teacherQueryWrapper.eq("tea_account", token.getUsername());
            TbTeacher tbTeacher = tbTeacherMapper.selectOne(teacherQueryWrapper);
            if (tbTeacher == null){

                return null;
            }

            //1.登录成功后的user对象 2.数据库的密码 3.登录成功后的username
            return new SimpleAuthenticationInfo(tbTeacher,tbTeacher.getTeaPassword(), ByteSource.Util.bytes(tbTeacher.getTeaSalt()),tbTeacher.getTeaAccount());
      }else if (token.getHost() == "3"){
            QueryWrapper<TbAdmin> adminQueryWrapper = new QueryWrapper<>();
            adminQueryWrapper.eq("admin_name", token.getUsername());
            TbAdmin tbAdmin = tbAdminMapper.selectOne(adminQueryWrapper);
            if (tbAdmin == null){

                return null;
            }
            //1.登录成功后的user对象 2.数据库的密码 3.登录成功后的username
            return new SimpleAuthenticationInfo(tbAdmin,tbAdmin.getAdminPwd(), ByteSource.Util.bytes(tbAdmin.getAdminSalt()),tbAdmin.getAdminName());
      }
        return null;
    }
}
